import { Component, OnInit } from '@angular/core';
import { HttpRequest } from "../../../service/httpRequest";
import { PageModel } from "../../../model/page-model";
import { flyIn } from '../../../animations/fly-in';
import {ActivatedRoute} from'@angular/router'
@Component({
  selector: 'app-single',
  templateUrl: './single.component.html',
  styleUrls: ['./single.component.css'],
  animations: [
    flyIn
  ]
})
export class SingleComponent implements OnInit {

  data:any;
  pageModel = new PageModel();
  oo:any = {devicename:'',province:'',city:'',district:''};
  followIds:any = {};
  page:number = 1;
  constructor(private httpRequest:HttpRequest,private activeRoute:ActivatedRoute) { }

  ngOnInit() {
    this.activeRoute.params.subscribe(params => {
      if (params['name'] != 'none') {
        this.oo.province = params['name'];
      }
      this.getData();
    });
  
  }

  getData(){
    this.httpRequest.sendGet('devices/page',{
      'page': this.page,
      'size': this.pageModel.itemsPerPage,
      'province':this.oo.province,
      'city':this.oo.city,
      'area':this.oo.district,
      'device_name':this.oo.devicename


    }).subscribe(res => {
      res = this.transData(res);
   
      this.data = res.result.aaData;
      this.pageModel.setPage({'iTotalRecords':res.result.iTotalRecords});
    },err => console.log(err));
  }



  follow(){
    let didstr="";
    let dids = Object.keys(this.followIds);
    for(let i=0;i<dids.length;i++){
      didstr += dids[i];
      if(dids.length!=(i+1)){
        didstr +=",";
      }
    }
    this.httpRequest.sendPost('devices/guanzhu',{
      'device_ids':didstr
    }).subscribe(res => {
      alert("关注成功！");
      this.followIds = {};
      this.getData();
    },err => console.log(err));
  }

  fn(e,id){
    let checkbox = e.target;
    if(checkbox.checked){
      this.followIds[id] = id;
    }else{
      delete this.followIds[id];
    }

  }


  transData(data){
    for (var i = 0; i < data.result.aaData.length; i++){

      switch (data.result.aaData[i].locate_mode){
        case '0':
          data.result.aaData[i].locate_mode = '输入方式';
          break;
        case '1':
          data.result.aaData[i].locate_mode = 'IP';
          break;
        case '2':
          data.result.aaData[i].locate_mode = 'GPIS';
          break;
        default:
          break;
      }

      switch (data.result.aaData[i].online){
        case '0' :
          data.result.aaData[i].online = '离线';
          break;
        case '1' :
          data.result.aaData[i].online = '在线';
          break;
        default:
          break;
      }
      data.result.aaData[i].did = data.result.aaData[i].id.replace("/","|");
    }

    return data;
  }
}
